/*
 * SLA.java
 */
package com.tovare.imap;

import java.util.*;
import javax.mail.*;

import com.sun.mail.imap.*;
import org.apache.commons.cli.*;

/**
 * 
 * 
 * @author toajacob Tov Are Jacobsen
 *
 */
public class SLA {
	
	
	public static String folderFlags(int flags){
		StringBuilder sb = new StringBuilder("[ ");
		if((flags & Folder.HOLDS_FOLDERS) != 0){
			sb.append("HOLDS_FOLDERS ");
		}
		if((flags & Folder.HOLDS_MESSAGES) != 0){
			sb.append("HOLDS_MESSAGES ");
		}
		sb.append("]");
		return sb.toString();
	}
	

	/**
	 * 
	 */
	public static void printFolderInfo(Folder folder){
		System.out.println("----------------------------------------");
		System.out.println("Name: " + folder.getFullName() );
		try{
	     if (!folder.isSubscribed())
	            System.out.println("Not Subscribed");
	        if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0){
	            System.out.println( "Total Messages:  " + folder.getMessageCount());
	            System.out.println("New Messages:    " + folder.getNewMessageCount());
	            System.out.println("Unread Messages: " + folder.getUnreadMessageCount());        	
	        }
	        if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0){
	        	 System.out.println( "Is Directory");
	        }
		}catch(MessagingException e){
			assert false;
		}
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws Exception  {

		// Command line options
		Options options = new Options();
		options.addOption( new Option("h", "help", false, "Display this help") );		
		options.addOption( new Option("p", "password", true, "account password for the specified user."));
		options.addOption( new Option("u", "user", true, "Username for this IMAP account."));
		options.addOption( new Option("d", "dir", false, "Print all the folders in this IMAP account."));
		CommandLineParser parser = new GnuParser();
		CommandLine cmd = parser.parse( options, args);
		
		if(cmd.hasOption("help")){
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp( "SLA", options );
		}

		if(cmd.hasOption("dir")){
			System.out.println("Printing out the directory tree.");
		}
		
		
        Properties props = System.getProperties();
        ResourceBundle bundle = ResourceBundle.getBundle("com.tovare.imap.sla");
        for(String key : bundle.keySet()){
        	props.setProperty(key, bundle.getString(key));
        } 
        Session session = Session.getDefaultInstance(props, null);

        assert bundle.getString("mail.imap.user") != null;
        assert bundle.getString("mail.imap.host") != null;
        assert bundle.getString("password") != null;

        IMAPStore store = (IMAPStore) session.getStore("imap");
        
        System.out.println("Attempting to connect");
        props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.imap.socketFactory.port", "993");

        store.connect(
        		props.getProperty("mail.imap.user"),
        		props.getProperty("password"));

        
        System.out.println("Connected");

        Folder defaultFolder = store.getDefaultFolder();
        
        printFolderInfo(defaultFolder);
        printFolderInfo(defaultFolder.getFolder("Inbox"));
        
  
  
        
        
        store.close();
        System.out.println("Disconnected");
	}

	
}
